<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/21 18:11
 * Remark:
 */

namespace App\Http\Controllers\Department;

use AlibabaCloud\SDK\Dingtalk\Vcrm_1_0\Models\DescribeRelationMetaResponseBody\relationMetaDTOList\items\props\dataSource\params\filters;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Consts\TableConsts\TableConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Http\Controllers\Controller;
use App\Http\Requests\Depart\AddDepartmentPost;
use App\Models\Position\PositionModel;
use App\Models\Staff\StaffDepartmentModel;
use App\Service\Department\DepartmentService;
use App\Tools\DingTalk\DingTalkDeptUserFacade;
use Illuminate\Http\Exceptions\HttpResponseException;

class DepartmentController extends Controller
{
    private DepartmentService $_departmentService;

    public function __construct()
    {
//        $this->middleware([
//            "departmentLeg"
//        ]);
        $this->_departmentService = new DepartmentService();

    }

    /**
     *新增部门
     * @param AddDepartmentPost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addDepartment(AddDepartmentPost $request): \Illuminate\Http\JsonResponse
    {
        $this->_departmentService->addDepartmentService($request);
        return ResponseFunc::returnJson(
            'ok',
            '',
            '新建部门成功',
            ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
        );
    }

    public function getDingTalkApprovalDepartmentList(){
        // TODO 这里不需要
        return ResponseFunc::returnJson(
            'ok',
            [],
            '获取部门列表成功',
            ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
        );
        // 获取当前用户
        $userInfo = TokenFuc::getCurrentStaffInfo();
        if(!$userInfo){
            return ResponseFunc::returnJson(
                'error',
                '',
                '请先登录',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
        $dingTalkId = $userInfo['dingTalkId'];
        $userDingTalkInfo = DingTalkDeptUserFacade::getDingTalkUserInfoByUserId($dingTalkId);
        $departIdList = $userDingTalkInfo->dept_id_list;
        $departData = [];

        foreach ($departIdList as $value){
            $deptInfo = DingTalkDeptUserFacade::getDepartmentInfoByDepartmentId($value);
            $departData[] = [
                'dept_id' => $deptInfo->dept_id,
                'name' => $deptInfo->name,
            ];
        }
        return ResponseFunc::returnJson(
            'ok',
            $departData,
            '获取部门列表成功',
            ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
        );
    }


    /**
     * 编辑部门
     * @param int $departmentId
     * @return \Illuminate\Http\JsonResponse
     */
    public function editDepartment(int $departmentId): \Illuminate\Http\JsonResponse
    {
        $editData = \request()->post();
        $editData['department_id'] = $departmentId;
        $this->_departmentService->editDepartmentService($editData);
        return ResponseFunc::returnJson(
            'ok',
            '',
            '编辑部门成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 删除部门
     * @param int $departmentId
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteDepartment(int $departmentId): \Illuminate\Http\JsonResponse
    {
        $this->_departmentService->patchEditDepartmentService($departmentId);
        return ResponseFunc::returnJson(
            'ok',
            '',
            '删除部门成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 获取部门详情
     * @param int $departmentId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDepartmentDetail(int $departmentId): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->_departmentService->getDepartmentInfoService($departmentId),
            '获取部门详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 通过公司Id获取部门下拉
     * @param $companyId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDeptSelectByCompanyId($companyId): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->_departmentService->getDeptSelectByCompanyIdService($companyId),
            '获取部门下拉成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * 获取当前公司部门下拉
     * @param $companyId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDeptSelectByCurrentCompany(): \Illuminate\Http\JsonResponse
    {
        $companyId = TokenFuc::getCurrentStaffInfo()['company_id'] ?? 0;;
        return ResponseFunc::returnJson(
            'ok',
            $this->_departmentService->getDeptSelectByCompanyIdService($companyId),
            '获取部门列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * @param $deptId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDeptCompanyIdByDeptId($deptId): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            [
                'company_id'  => $this->_departmentService->getDeptCompanyIdByDeptId($deptId)
            ],
            '获取部门列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    public function getStaffDepartmentList(){
        $staffInfo = TokenFuc::getCurrentStaffInfo();
        // 获取当前用户所有的部门
        $staffDepartmentModel = new StaffDepartmentModel();
        // 职位
        $positionModel = new PositionModel();
        // 职位ID
        $staffDepartmentSelect = $staffDepartmentModel
            ->select([
                'd.department_id',
                'department_name',
                'position_id',
                'is_master'
            ])
            ->leftJoin(TableConsts::CY_DEPARTMENT_TABLE_NAME." as d",'d.department_id','staff_department.department_id')
            ->where('staff_id',$staffInfo['staff_id'])
            ->get()->toArray();
        foreach($staffDepartmentSelect as $key => $value){
            $staffDepartmentSelect[$key]['default'] = false;
            if($value['department_id'] == $staffInfo['department_id']){
                $staffDepartmentSelect[$key]['is_default'] = true;
            }
            $staffDepartmentSelect[$key]['position_name'] = $positionModel->getPositionInfoByPositionId($value['position_id'])['position_name'] ?? "-";
        }
        return ResponseFunc::returnJson(
            'ok',
            $staffDepartmentSelect,
            '获取部门列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }


}